package FIle;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

public class FileMoveAndRename {
    /**
     * 重命名文件或移动文件示例代码 * @param args
     */
    public static void main(String[] args) throws IOException { //绝对路径重命名文件
//        File_rename_shiyan();
        File_move();
//        File_all_file();
    }

    /**
     * 实验的重名操作(统一文件夹格式即可)
     */
    public static void File_rename_shiyan() {
        String source = "C:\\Users\\wj\\Desktop\\实验-源文件处理\\23人\\考核";
        File file1 = new File(source);
        for (File fileName : file1.listFiles()) {
            if (fileName.isFile()) {
                String name_file = fileName.getName();
                String[] split = name_file.split("（")[0].split("-");
                String newFileName = split[0] + "-" + split[1] + "-考核.pdf";
                System.out.println("newFileName = " + newFileName);
                // 创建名称
                File fileNew = new File(source + "\\" + newFileName);
//                System.out.println("fileNew.getAbsolutePath() = " + fileNew.getAbsolutePath());
                // 重命名
                fileName.renameTo(fileNew); // 同一路径，不同问价名（重命名）


            }

        }
    }


    // 一个总的文件夹,然后在区分
    public static void File_move() throws IOException {// 将文件从一个目录移动到另一个目录
        String source = "C:\\Users\\wj\\Desktop\\实验-源文件处理\\总";
        String dest = "C:\\Users\\wj\\Desktop\\汇总\\实验报告";
        File source_file = new File(source);
        File dest_File = new File(dest);

        HashMap<String, String> Directory = new HashMap<>();
        // 文件夹的map，用于renameto
        for (File fileD : dest_File.listFiles()) { // 全部文件
            if (fileD.isDirectory()) {
                String AbsolutePath = fileD.getAbsolutePath();
                String Name = fileD.getName().split("-")[0];
//                System.out.println("name = " + Name);
                Directory.put(Name,AbsolutePath);
            }
        }
//        System.out.println("Directory.toString() = " + Directory.toString());

        // 文件的map，用于renameto
        for (File fileS : source_file.listFiles()) { // 全部文件
            if (fileS.isFile()) {
//                System.out.println("fileS = " + fileS);
                String AbsolutePath = fileS.getAbsolutePath();
                String fileName = fileS.getName();
//                System.out.println("name = " + fileS.getName());
                String keyFileName = fileS.getName().split("-")[0];
                String newFileName = Directory.get(keyFileName) + "\\" + fileName;
                File newFile = new File(newFileName);
//                File.put(Name,AbsolutePath);
                System.out.println("newFileName = " + newFileName);
                fileS.renameTo(newFile);
            }
        }
    }

    public static void File_all_file() throws IOException {// 将文件从一个目录移动到另一个目录
        String source_D = "C:\\Users\\wj\\Desktop\\实验-源文件处理\\2339";
        String dest_D = "C:\\Users\\wj\\Desktop\\汇总\\实验报告";
        File file = new File(source_D);
        File destFile = new File(dest_D);

        for (File file1 : file.listFiles()) { // 全部文件
            if (file1.isFile()) {
                String[] split = file1.getName().split("-");
                String name = split[0] + "-" + split[1];
//                System.out.println("name = " + name);
                String newDirect = dest_D + "\\" + name;
                System.out.println("newDirect = " + newDirect);
                File file2 = new File(newDirect);
                boolean newFile = file2.mkdirs();
            }
        }
    }
}
